﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity = VietStar.Library.Entity;
using Business = VietStar.Library.Business;
using System.IO;

public partial class controls_admin_listtainguyen : System.Web.UI.UserControl
{
    Entity.HeDieuHanh clsE_HeDieuHanh = new VietStar.Library.Entity.HeDieuHanh();
    Business.HeDieuHanh clsB_HeDieuHanh = new VietStar.Library.Business.HeDieuHanh();
    Entity.Driver clsE_Driver = new VietStar.Library.Entity.Driver();
    Business.Driver clsB_Driver = new VietStar.Library.Business.Driver();
    Entity.TaiNguyen clsE_TaiNguyen = new VietStar.Library.Entity.TaiNguyen();
    Business.TaiNguyen clsB_TaiNguyen = new VietStar.Library.Business.TaiNguyen();
    Entity.NhaSanXuat clsE_NhaSanXuat = new VietStar.Library.Entity.NhaSanXuat();
    Business.NhaSanXuat clsB_NhaSanXuat = new VietStar.Library.Business.NhaSanXuat();
    PagedDataSource _PageDataSource = new PagedDataSource();
    private static DataTable dtTaiNguyen = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Load_HeDieuHanh(0);
            Alert.Load_DropDownList(ddlDriver, clsB_Driver.Select(), "TenDriver", "Id", "------Chọn driver + phần mềm------");
            Alert.Load_DropDownList(ddlNhaSanXuat, clsB_NhaSanXuat.Select(), "TenNhaSanXuat", "Id", "------Chọn nhà sản xuất------");
            dtTaiNguyen = clsB_TaiNguyen.Select(clsE_TaiNguyen);
            Load_TaiNguyen();
        }
    }
    private void Load_HeDieuHanh(int IdHeDieuHanh)
    {
        clsE_HeDieuHanh.Id = IdHeDieuHanh;
        DataTable dt = clsB_HeDieuHanh.SelectTheoCap(clsE_HeDieuHanh);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                int id = (int)dr["Id"];
                string name = dr["Ten"].ToString();
                int level = (int)dr["Cap"];
                string sub = "";
                for (int i = 1; i <= level; i++)
                {
                    sub += "---";
                }
                name = sub + name;
                ListItem item = new ListItem(name, id.ToString());
                ddlHeDieuHanh.Items.Add(item);
                Load_HeDieuHanh(id);
            }
        }
    }
    
    private void Load_TaiNguyen()
    {
        if (dtTaiNguyen.Rows.Count == 0)
        {
            BindItemsList(dtTaiNguyen);
            PhanTrang.Visible = true;
            msgNoRows.Style.Add("display", "block");
        }
        else if (dtTaiNguyen.Rows.Count > 0 && dtTaiNguyen.Rows.Count <= 3)
        {
            BindItemsList(dtTaiNguyen);
            PhanTrang.Visible = false;
        }
        else if (dtTaiNguyen.Rows.Count > 3)
        {
            BindItemsList(dtTaiNguyen);
            PhanTrang.Visible = true;
        }
    }
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];

            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private void BindItemsList(DataTable dt)
    {
        DataTable dataTable = dt;
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 3;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.rptDanhSachTaiNguyen.DataSource = _PageDataSource;
        this.rptDanhSachTaiNguyen.DataBind();
        this.doPaging();
    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        fistIndex = CurrentPage - 5;
        if (CurrentPage > 5)
        {
            lastIndex = CurrentPage + 5;
        }
        else
        {
            lastIndex = 10;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 10;
        }
        if (fistIndex < 0)
        {
            fistIndex = 0;
        }
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }
    private int fistIndex
    {
        get
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {
            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/addtainguyen.aspx?action=add&info=0");
    }
    protected void btnLamMoi_Click(object sender, EventArgs e)
    {
        dtTaiNguyen = clsB_TaiNguyen.Select(clsE_TaiNguyen);
        Load_TaiNguyen();
        txtTuKhoa.Text = "Tìm kiếm driver";
        ddlDriver.SelectedValue = "0";
        ddlHeDieuHanh.SelectedValue = "0";
        ddlNhaSanXuat.SelectedValue = "0";
    }
    protected void btnTim_Click(object sender, EventArgs e)
    {
        clsE_TaiNguyen.TenThietBi = txtTuKhoa.Text.Trim();
        clsE_TaiNguyen.IdHeDieuHanh = Convert.ToInt32(ddlHeDieuHanh.SelectedValue);
        clsE_TaiNguyen.IdDriver = Convert.ToInt32(ddlDriver.SelectedValue);
        clsE_TaiNguyen.IdNhaSanXuat = Convert.ToInt32(ddlNhaSanXuat.SelectedValue);
        if (txtTuKhoa.Text == "Tìm kiếm driver" && ddlDriver.SelectedValue == "0" && ddlHeDieuHanh.SelectedValue!="0" && ddlNhaSanXuat.SelectedValue=="0")
        {
            TimKiem(0);
        }
        else if (txtTuKhoa.Text == "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue == "0" && ddlNhaSanXuat.SelectedValue == "0")
        {
            TimKiem(1);
        }
        else if (txtTuKhoa.Text == "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue != "0" && ddlNhaSanXuat.SelectedValue == "0")
        {
            TimKiem(2);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue == "0" && ddlHeDieuHanh.SelectedValue == "0" && ddlNhaSanXuat.SelectedValue == "0")
        {
            TimKiem(3);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue == "0" && ddlHeDieuHanh.SelectedValue != "0" && ddlNhaSanXuat.SelectedValue == "0")
        {
            TimKiem(4);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue == "0" && ddlNhaSanXuat.SelectedValue == "0")
        {
            TimKiem(5);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue != "0" && ddlNhaSanXuat.SelectedValue == "0")
        {
            TimKiem(6);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue == "0" && ddlHeDieuHanh.SelectedValue != "0" && ddlNhaSanXuat.SelectedValue != "0")
        {
            TimKiem(7);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue == "0" && ddlNhaSanXuat.SelectedValue == "0")
        {
            TimKiem(8);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue != "0" && ddlNhaSanXuat.SelectedValue != "0")
        {
            TimKiem(9);
        }
        else if (txtTuKhoa.Text != "Tìm kiếm driver" && ddlDriver.SelectedValue == "0" && ddlHeDieuHanh.SelectedValue == "0" && ddlNhaSanXuat.SelectedValue != "0")
        {
            TimKiem(10);
        }
        else if (txtTuKhoa.Text == "Tìm kiếm driver" && ddlDriver.SelectedValue == "0" && ddlHeDieuHanh.SelectedValue == "0" && ddlNhaSanXuat.SelectedValue != "0")
        {
            TimKiem(11);
        }
        else if (txtTuKhoa.Text == "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue == "0" && ddlNhaSanXuat.SelectedValue != "0")
        {
            TimKiem(12);
        }
        else if (txtTuKhoa.Text == "Tìm kiếm driver" && ddlDriver.SelectedValue != "0" && ddlHeDieuHanh.SelectedValue != "0" && ddlNhaSanXuat.SelectedValue != "0")
        {
            TimKiem(13);
        }
        else if (txtTuKhoa.Text == "Tìm kiếm driver" && ddlDriver.SelectedValue == "0" && ddlHeDieuHanh.SelectedValue != "0" && ddlNhaSanXuat.SelectedValue != "0")
        {
            TimKiem(14);
        }
        else
        {
            Alert.Show("Bạn chưa nhập từ khóa hoặc chưa chọn chuyên mục cần tìm");
            dtTaiNguyen = clsB_TaiNguyen.Select(clsE_TaiNguyen);
            Load_TaiNguyen();
        }
    }
    private void TimKiem(int Opt)
    {
        clsE_TaiNguyen.Option = Opt;
        dtTaiNguyen = clsB_TaiNguyen.SelectTimKiem(clsE_TaiNguyen);
        Load_TaiNguyen();
    }
    protected void rptDanhSachTaiNguyen_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "xoa")
            {
                bool delete = false;
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                clsE_TaiNguyen.Id = id;
                DataTable dssp = clsB_TaiNguyen.SelectPrimarykey(clsE_TaiNguyen);
                File.Delete(Server.MapPath("~/images/tainguyen/" + dssp.Rows[0]["HinhAnh"]));
                delete = clsB_TaiNguyen.Delete(clsE_TaiNguyen);
                if (delete == true)
                {
                    Alert.Show("Xóa tài nguyên thành công!");
                    dtTaiNguyen = clsB_TaiNguyen.Select(clsE_TaiNguyen);
                    Load_TaiNguyen();
                }
                else
                {
                    Alert.Show("Xóa tài nguyên không thành công!");
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindItemsList(dtTaiNguyen);
    }
   
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindItemsList(dtTaiNguyen);
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindItemsList(dtTaiNguyen);
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = Convert.ToInt32(ViewState["TotalPages"]) - 1;
        BindItemsList(dtTaiNguyen);
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
         LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
         if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
         {
             lnkbtnPage.Enabled = false;
             //lnkbtnPage.Style.Add("fone-size", "14px");
             //lnkbtnPage.Font.Bold = true;
             lnkbtnPage.CssClass = "active";
         }
    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
            BindItemsList(dtTaiNguyen);
        }
    }
}
